package com.jeeplus.juhe;

import org.springframework.util.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * Created by hadoop on 16/4/11.
 */
public class AesUtil {

	// 加密
	public static String encrypt(String sSrc, String sKey) {
		if (StringUtils.isEmpty(sSrc)) {
			return "";
		}
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes("utf-8"), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
			return new String(Base64.getEncoder().encode(encrypted), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String encrypt(String sSrc) {
		return encrypt(sSrc, RNAUtil.APPKEY);
	}

	// 解密
	public static String decrypt(String input, String key) {
		if (StringUtils.isEmpty(input)) {
			return "";
		}
		try {
			SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skey);
			byte[] output = cipher.doFinal(Base64.getDecoder().decode(input));
			return new String(output);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return null;
	}

	public static String encryptCBC(String data) {
		return encryptCBC(data, RNAUtil.APPKEY);
	}

	public static String encryptCBC(String data, String key) {
		if (StringUtils.isEmpty(data)) {
			return "";
		}
		try {
			String iv = Md5Util.MD5_16(key); // iv不一定要去md5 16位, 加密解密端约定相同即可

			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
			int blockSize = cipher.getBlockSize();

			byte[] dataBytes = data.getBytes();
			int plaintextLength = dataBytes.length;
			if (plaintextLength % blockSize != 0) {
				plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
			}

			byte[] plaintext = new byte[plaintextLength];
			System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

			SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
			IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

			cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
			byte[] encrypted = cipher.doFinal(plaintext);

			return Base64.getEncoder().encodeToString(encrypted);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String decrypt(String input) {
		return decrypt(input, RNAUtil.APPKEY);
	}

	public static String decryptCBC(String data) {
		return decryptCBC(data, RNAUtil.APPKEY);
	}

	public static String decryptCBC(String data, String key) {
		if (StringUtils.isEmpty(data)) {
			return "";
		}
		try {
			String iv = Md5Util.MD5_16(key); // iv不一定要去md5 16位, 加密解密端约定相同即可

			byte[] encrypted1 = Base64.getDecoder().decode(data);

			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
			SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
			IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

			cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original);
			return originalString.trim();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}